//
//  RZMessageCellTool.m
//  RZIM
//
//  Created by reyzhang on 2021/8/19.
//

#import "RZMessageCellTool.h"
#import "RZKitCommonDefine.h"

//消息单元格类型
typedef NSString * RZMessageCellType NS_STRING_ENUM;
/**
 消息展示cell 类型
 */
static RZMessageCellType const RZMessageCellTypeTextMessage = @"RZTextMessageCell";
static RZMessageCellType const RZMessageCellTypeTipMessage =  @"RZTipMessageCell";
static RZMessageCellType const RZMessageCellTypeVoiceMessage = @"RZVoiceMessageCell";
static RZMessageCellType const RZMessageCellTypeImageMessage = @"RZImageMessageCell";
static RZMessageCellType const RZMessageCellTypeVideoMessage = @"RZVideoMessageCell";



@implementation RZMessageCellTool


+ (UIImage *)getDefaultMessageCellBackgroundImage:(RZMessageInfo *)model {
    UIImage *bubbleImage;
    if (MessageDirection_RECEIVE == model.messageDirection) {
        bubbleImage = RZResourceImage(@"chat_from_bg_normal");
    } else {
        bubbleImage = RZResourceImage(@"chat_to_bg_normal");
    }
    
    bubbleImage = [self getResizableImage:bubbleImage];
    return bubbleImage;
}

/**
 图片拉伸
 */
+ (UIImage *)getResizableImage:(UIImage *)image{
    return [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.5, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5)];
}


+ (CGFloat)getMessageContentViewMaxWidth{
    float screenRatio = 0.637;
    if (SCREEN_WIDTH <= 320) {
        screenRatio = 0.6;
    }
    float maxWidth = (int)(SCREEN_WIDTH * screenRatio) + 7;
    return maxWidth;
}


+ (NSDictionary *)messageCellClassMap {

    NSDictionary *classMap =  @{
        //文本消息
        [self getMessageCellKey:MessageType_TEXT] : RZMessageCellTypeTextMessage,
        
        //系统消息
        [self getMessageCellKey:MessageType_SYS_TIME] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_GROUPNEW] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_DISSGROUP] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_INVITEGROUP] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_KICKGROUP] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_LEAVEGROUP] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_SPEAKFORBID] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_SPEAKRELIEVE] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_RECALL] : RZMessageCellTypeTipMessage,
        [self getMessageCellKey:MessageType_SYS_EDITGROUPNAME] : RZMessageCellTypeTipMessage,
        
        //语音消息
        [self getMessageCellKey:MessageType_AUDIO] : RZMessageCellTypeVoiceMessage,
        //图片消息
        [self getMessageCellKey:MessageType_PICTURE] : RZMessageCellTypeImageMessage,
        //视频消息
        [self getMessageCellKey:MessageType_VIDEO] : RZMessageCellTypeVideoMessage,
    };
    
    return classMap;
}

+ (NSString *)getMessageCellKey:(RZMessageType)messageType {
    return [NSString stringWithFormat:@"messagetype_%ld",(long)messageType];
}

+ (CGFloat)referenceExtraHeight:(Class)cellClass messageModel:(RZMessageInfo *)model {
    CGFloat extraHeight = BASE_CONTENT_VIEW_BOTTOM;

    if ([cellClass isSubclassOfClass:RZMessageCell.class]) {
        // name label height
        if (model.isDisplayNickname && model.messageDirection == MessageDirection_RECEIVE) {
            extraHeight += NameHeight + NameAndContentSpace;
        }
    }
    return extraHeight;
}


+ (CGSize)getThumbnailImageSize:(UIImage *)image{
    return [self getThumbnailImageSizeWithOrignalSize:image.size];
}



/**
 根据原有图片大小，取缩略后的显示大小
 */
+ (CGSize)getThumbnailImageSizeWithOrignalSize:(CGSize)orignalImageSize {
    //图片消息最小值为 100 X 100，最大值为 240 X 240
    // 重新梳理规则，如下：
    // 1、宽高任意一边小于 100 时，如：20 X 40 ，则取最小边，按比例放大到 100 进行显示，如最大边超过240 时，居中截取 240
    // 进行显示
    // 2、宽高都小于 240 时，大于 100 时，如：120 X 140 ，则取最长边，按比例放大到 240 进行显示
    // 3、宽高任意一边大于240时，分两种情况：
    //(1）如果宽高比没有超过 2.4，等比压缩，取长边 240 进行显示。
    //(2）如果宽高比超过 2.4，等比缩放（压缩或者放大），取短边 100，长边居中截取 240 进行显示。
    CGSize imageSize = orignalImageSize;
    CGFloat imageMaxLength = 120;
    CGFloat imageMinLength = 50;
    if (imageSize.width == 0 || imageSize.height == 0) {
        return CGSizeMake(imageMaxLength, imageMinLength);
    }
    CGFloat imageWidth = 0;
    CGFloat imageHeight = 0;
    if (imageSize.width < imageMinLength || imageSize.height < imageMinLength) {
        if (imageSize.width < imageSize.height) {
            imageWidth = imageMinLength;
            imageHeight = imageMinLength * imageSize.height / imageSize.width;
            if (imageHeight > imageMaxLength) {
                imageHeight = imageMaxLength;
            }
        } else {
            imageHeight = imageMinLength;
            imageWidth = imageMinLength * imageSize.width / imageSize.height;
            if (imageWidth > imageMaxLength) {
                imageWidth = imageMaxLength;
            }
        }
    } else if (imageSize.width < imageMaxLength && imageSize.height < imageMaxLength &&
               imageSize.width >= imageMinLength && imageSize.height >= imageMinLength) {
        if (imageSize.width > imageSize.height) {
            imageWidth = imageMaxLength;
            imageHeight = imageMaxLength * imageSize.height / imageSize.width;
        } else {
            imageHeight = imageMaxLength;
            imageWidth = imageMaxLength * imageSize.width / imageSize.height;
        }
    } else if (imageSize.width >= imageMaxLength || imageSize.height >= imageMaxLength) {
        if (imageSize.width > imageSize.height) {
            if (imageSize.width / imageSize.height < imageMaxLength / imageMinLength) {
                imageWidth = imageMaxLength;
                imageHeight = imageMaxLength * imageSize.height / imageSize.width;
            } else {
                imageHeight = imageMinLength;
                imageWidth = imageMinLength * imageSize.width / imageSize.height;
                if (imageWidth > imageMaxLength) {
                    imageWidth = imageMaxLength;
                }
            }
        } else {
            if (imageSize.height / imageSize.width < imageMaxLength / imageMinLength) {
                imageHeight = imageMaxLength;
                imageWidth = imageMaxLength * imageSize.width / imageSize.height;
            } else {
                imageWidth = imageMinLength;
                imageHeight = imageMinLength * imageSize.height / imageSize.width;
                if (imageHeight > imageMaxLength) {
                    imageHeight = imageMaxLength;
                }
            }
        }
    }
    return CGSizeMake(imageWidth, imageHeight);
}

@end
